<?php
class Admin_ProductImageController extends Apps_Controller_Action_Admin {

    public function indexAction() {
        $pId = $this->_request->getParam('id');

        $tbProduct = new Model_DbTable_Product();

        $product = $tbProduct->fetchRow(array('id =?' => $pId));

        if(!$pId || !$product) {
            $this->_redirect('/admin');
        }

        // Thumbnail
        $product = $product->toArray();
        $this->view->thumbnail = $product['thumbnail_link'];

        $tbProductImage = new Model_DbTable_ProductImage();
        $images = $tbProductImage->fetchAll(array('product_id =?' => $pId), array('asc' => 'sorting_number'))->toArray();
        $this->view->images = $images;

        if ($this->_request->isPost()) {
            // Delete images
            $itemchecker = $this->_request->getParam('item_checker');
            if(is_array($itemchecker)) {
                foreach ($itemchecker as $item) {
                    $tbProductImage->delete(array('id =?' => $item));
                }
            }

            // Update sorting number
            $sortingNumber = $this->_request->getParam('sorting_number');
            if($sortingNumber) {
                foreach ($sortingNumber as $k => $sortingItem) {
                    $tbProductImage->safelyUpdate(array('sorting_number' => $sortingItem), array('id =?' => $images[$k]['id']));
                }
            }

            // Upload thumbnail and images
            $thumbnail = null;
            $images    = array();

            $uploader = new Apps_Plugin_Upload_File();
            $uploader->setSubCategory(Apps_Plugin_Upload_File::CAT_PRODUCT);
            $uploaded = $uploader->upload();

            //Zend_Debug::dump($uploaded);die;

            // Resize images
            if($uploaded) {
                $imageResize = new Apps_Plugin_ResizeImages();

                foreach($uploaded as $key => $imageUploaded) {
                    if($key != 'thumbnail_link') {
                        $imageLink = $imageUploaded['uri'];
                        try {
                            $imageResize->load(PUBLIC_PATH . $imageLink);
                            $imageResize->resize(550, 550);
                            $imageResize->save(PUBLIC_PATH . $imageLink);
                        } catch (Exception $e) {
                            new Zend_Exception('Resize images error');
                        }
                        $images[] = $imageLink;
                    }
                }

                // Resize thumbnail
                if(isset($uploaded['thumbnail_link'])) {
                    $thumbnail = $uploaded['thumbnail_link']['uri'];
                    try {
                        $imageResize->load(PUBLIC_PATH . $thumbnail);
                        $imageResize->resize(184, 220);
                        $imageResize->save(PUBLIC_PATH . $thumbnail);
                    } catch (Exception $e) {
                        new Zend_Exception('Resize thumbnail error');
                    }
                }
            }

            // Update thumbnail
            if($thumbnail) {
                $tbProduct->safelyUpdate(array('thumbnail_link' => $thumbnail), array('id =?' => $pId));
            }

            // Insert images product
            if($images) {
                foreach ($images as $key => $link) {
                    $tbProductImage->safelyInsert(array('product_id' => $pId, 'path' => $link, 'sorting_number' => $key));
                }
            }
            $this->_flashMessenger->addMessage(self::$msgAddSuccess);

            $this->_redirect('/admin/product-image/index/id/'. $pId);
        }
    }
}